<template>
  <div class="permission">
    <el-col :span="24" class="action-bar">
			<el-button type="primary" size="medium" @click="addBtnReset" style="margin: 0 20px;">新增</el-button>
    </el-col>

    <!--列表-->
		<el-table :data="permissionsList" highlight-current-row class="el-table-style">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60" sortable>
			</el-table-column>
      <el-table-column prop="display_name" label="权限名称" width="250" sortable>
			</el-table-column>
			<el-table-column prop="description" label="权限描述" width="150" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template slot-scope="scope">
					<el-button type="primary" size="mini" @click="permissionsSet(scope.row.id)">编辑</el-button>
					<el-button type="danger" class="danger-btn" size="mini" @click="delPermissions(scope.row.id)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-col :span="24" class="footer-bar">
			<!-- <div class="batch"><el-button size="medium ">批量删除</el-button></div> -->
			<div class="page-code">
				<el-pagination 
					background 
					layout="prev, pager, next" 
					:total="pageTotal"
					:page-size="limit"
					@current-change="getPermissionsList"
				>
				</el-pagination>
			</div>
    </el-col>

    <!-- 新增 -->
		<el-dialog title="新增权限" :visible.sync="addFormVisible" width="40%">
			<el-form :model="addForm" label-width="100px">
				<el-form-item label="权限名称">
          <el-input v-model="addForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="权限显示名称">
          <el-input v-model="addForm.permissionName" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="权限描述">
          <el-input type="textarea" v-model="addForm.permissionDsc" :autosize="{ minRows: 3, maxRows: 5}"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="cancelBtn">取消</el-button>
				<el-button type="primary" @click.native="addPermissionsSubmit">提交</el-button>
			</div>
		</el-dialog>

    <!-- 编辑 -->
		<el-dialog title="编辑权限" :visible.sync="editFormVisible" width="40%">
			<el-form :model="addForm" label-width="100px">
				<el-form-item label="权限名称" prop="characterName">
          <el-input v-model="editForm.permissionName" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="权限描述" prop="characterPermission">
          <el-input type="textarea" v-model="editForm.permissionDsc" :autosize="{ minRows: 3, maxRows: 5}"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editPermissionsSubmit">提交</el-button>
			</div>
		</el-dialog>
  </div>
</template>

<script>
export default {
  data(){
    return{
			permissions:[],
			editForm: {
				id:'',
				permissionName:'',
				permissionDsc: '',
				name:'',
			},
			pageTotal:0,
			limit:16,
      radio: 3,
      addFormVisible: false,//新增界面是否显示
			editFormVisible: false,//编辑界面是否显示
			permissionsList:[],
      //新增界面数据
			addForm: {
				name:'',
				permissionName:'',
				permissionDsc: '',
      },
    }
	},
	mounted(){
		this.getPermissionsList();
	},
  methods:{
    //获取编辑列表
    async getPermissionsList(page){
			let params = {
				limit:this.limit,
				page:page || 1,
			}
      const res = await this.$api.getPermissionsList(params);
      if(res.data.level=='success'){
        let list = res.data.data;      
				this.permissionsList = list;
				this.pageTotal = res.data.page_info.total
      }else{
        this.$message({
          message: res.data.message,
          type: 'warning'
        });
      }
		},
		//编辑权限
    async permissionsSet(id){
      const params = {
        id:id,
			}
      const res = await this.$api.permissionsSet(params);
      if(res.data.level=='success'){
				let permissions = res.data.data;
				this.editFormVisible = true;
				this.editForm.id = permissions.id;
				this.editForm.permissionName = permissions.display_name;
				this.editForm.permissionDsc = permissions.description;
				this.editForm.name = permissions.name;
      }else{
        this.$message({
          message: res.data.message,
          type: 'warning'
        });
      }
		},
		//编辑提交按钮
		async editPermissionsSubmit(){
			const params = {
				id:this.editForm.id,
				name:this.editForm.name,
				display_name:this.editForm.permissionName,
				description:this.editForm.permissionDsc,
			}
      const res = await this.$api.addPermissions(params);
      if(res.data.level=='success'){
				this.$message({
          message: '修改成功',
          type: 'success'
				});
				this.windowInit();
				this.getPermissionsList();
      }else{
        this.$message({
          message: res.data.message,
          type: 'warning'
        });
      }
		},
		//编辑和新增取消按钮
		cancelBtn(){
			this.windowInit();
		},
		//初始化编辑和新增窗口
		windowInit(){
			this.editForm.id = "";
			this.editForm.name = "";
			this.editForm.permissionName = "";
			this.editForm.permissionDsc = "";
			this.editFormVisible = false;
			this.addForm.name = "";
			this.addForm.permissionName = "";
			this.addForm.permissionDsc = "";
			this.addFormVisible = false;
		},
		//新增按钮重置表单
		addBtnReset(){
			this.addForm.name = '';
			this.addForm.permissionName = '';
			this.addForm.permissionDsc = '';
			this.addFormVisible = true;
		},
		//添加权限按钮
		async addPermissionsSubmit(){
			const params = {
				name:this.addForm.name,
				display_name:this.addForm.permissionName,
				description:this.addForm.permissionDsc,
			}
      const res = await this.$api.addPermissions(params);
      if(res.data.level=='success'){
				this.$message({
          message: '添加成功',
          type: 'success'
				});
				this.windowInit();
				this.getPermissionsList();
      }else{
        this.$message({
          message: res.data.message,
          type: 'warning'
        });
      }
		},
		//删除权限
		async delPermissions(id){
			const params = {
        id:id,
			}
			//删除
			const judge= this.$confirm('删除该权限?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
				}).then(async() => {
					let res = await this.$api.delPermissions(params)
					if(res.data.level=='success'){
						this.getPermissionsList();
						this.$message({
							type: 'success',
							message: res.data.message
						});
					}else{
						this.$message({
							type: 'error',
							message: res.data.message
						});
					}
				}).catch(() => {
			});
		}
	}
}
</script>
<style lang='scss' scoped>
  .permission{
    .action-bar{
			background-color: #f2f2f2;
			height: 60px;
      margin: 10px 0px;
      box-sizing: border-box;
			display: flex;
			align-items: center;
			padding-left: 20px;
			.action-bar-input{
				width: 250px;
				margin-right: 14px;
			}
		}
		.el-table-style{
			width: 100%;
			border: 1px solid #f2f2f2;
		}
		.footer-bar{
			display: flex;
			justify-content: space-between;
			align-items: center;
      background: #f2f2f2;
      padding: 8px;
      margin: 10px 0px;
      box-sizing: border-box;
		}
		.dialog-footer{
			text-align: center;
		}
  }
</style>

<style>
	.el-pagination.is-background .el-pager li,
	.el-pagination.is-background .btn-prev,
	.el-pagination.is-background .btn-next
	{
		background-color: #fff;
		border: 1px solid #f1f1f1;
		margin: 0;
	}
  .member .el-dialog__body{overflow: auto;}
    .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }


</style>